package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Text;
	import net.flashpunk.utils.Input;
	import net.flashpunk.utils.Key;
	import net.flashpunk.FP;
	/**
	 * © 2012 TheJavaArcade.com
	 */
	public class NameText extends Entity {
		
		public static var letterCount:int;
		public static var nameStr:String;
		public static var theText:Text;
		
		public function NameText() {
			theText = new Text("");
			graphic = theText;
			layer = -2;
			y = FP.camera.y + 80;
			nameStr = "_";
			letterCount = 0;
			if (Assets.userTypeStr == "user") {
				nameStr = Assets.userNameStr;
				letterCount = nameStr.length;
			}
			if (Assets.userTypeStr == "guest") {
				letterCount = Assets.userNameStr.length;
				if (Assets.userNameStr.length < 10) {
					nameStr = Assets.userNameStr + "_";
				} else {
					nameStr = Assets.userNameStr;
				}
			}
		}
		override public function update():void {//UPDATE
			if (Assets.userTypeStr == "guest") enterName();
			theText.text = nameStr;
			x = FP.camera.x+(FP.halfWidth / 2) - (theText.width / 2);
		}
		
		public function enterName():void {//ENTER NAME
			var letter:String;
			letter = "";
			if (Input.pressed(Key.A)) letter = "a"; 
			if (Input.pressed(Key.B)) letter = "b";
			if (Input.pressed(Key.C)) letter = "c";
			if (Input.pressed(Key.D)) letter = "d";
			if (Input.pressed(Key.E)) letter = "e";
			if (Input.pressed(Key.F)) letter = "f";
			if (Input.pressed(Key.G)) letter = "g";
			if (Input.pressed(Key.H)) letter = "h";
			if (Input.pressed(Key.I)) letter = "i";
			if (Input.pressed(Key.J)) letter = "j";
			if (Input.pressed(Key.K)) letter = "k";
			if (Input.pressed(Key.L)) letter = "l";
			if (Input.pressed(Key.M)) letter = "m";
			if (Input.pressed(Key.N)) letter = "n";
			if (Input.pressed(Key.O)) letter = "o";
			if (Input.pressed(Key.P)) letter = "p";
			if (Input.pressed(Key.Q)) letter = "q";
			if (Input.pressed(Key.R)) letter = "r";
			if (Input.pressed(Key.S)) letter = "s";
			if (Input.pressed(Key.T)) letter = "t";
			if (Input.pressed(Key.U)) letter = "u";
			if (Input.pressed(Key.V)) letter = "v";
			if (Input.pressed(Key.W)) letter = "w";
			if (Input.pressed(Key.X)) letter = "x";
			if (Input.pressed(Key.Y)) letter = "y";
			if (Input.pressed(Key.Z)) letter = "z";
			if (Input.pressed(Key.DIGIT_0)) letter = "0";
			if (Input.pressed(Key.DIGIT_1)) letter = "1";
			if (Input.pressed(Key.DIGIT_2)) letter = "2";
			if (Input.pressed(Key.DIGIT_3)) letter = "3";
			if (Input.pressed(Key.DIGIT_4)) letter = "4";
			if (Input.pressed(Key.DIGIT_5)) letter = "5";
			if (Input.pressed(Key.DIGIT_6)) letter = "6";
			if (Input.pressed(Key.DIGIT_7)) letter = "7";
			if (Input.pressed(Key.DIGIT_8)) letter = "8";
			if (Input.pressed(Key.DIGIT_9)) letter = "9";
			if (Input.pressed(Key.SPACE)) letter = " ";
			if (Input.pressed(Key.BACKSPACE)) letter = "-";
			nameStr = nameStr.substr(0, letterCount);//remove the underscore
			if (letter != "") {
				if (letter == "-") {//delete
					letterCount--;
					if (letterCount < 0) letterCount = 0;
					nameStr = nameStr.substr(0, letterCount);
				}else if(letterCount < 10){
					nameStr += letter;
					letterCount++;
				}
			}
			if (letterCount < 10) nameStr += "_";
		}
		
	}

}